package com.project.frba.bifront.client.view.properties;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.ui.Composite;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.project.frba.bifront.client.view.CubePanel;

public class PropertySelector extends Composite {

	private Window mainWindow = new Window();
	private PropertyPanel cPanel;
	private PropertyPanel rPanel;
	private AvailablePropertyPanel aPanel;
	private CubePanel cubePanel;
	private Button btSave = new Button("Guardar");
	private Button btCancel = new Button("Cancelar");
		
	public PropertySelector() {

	}

	public PropertySelector(CubePanel cubePanel) {
		mainWindow.setLayout(new HorizontalLayout(6));
		mainWindow.setTitle("Configurar");
		mainWindow.setWidth(970);
		mainWindow.setResizable(false);
		mainWindow.setBorder(false);
		mainWindow.setPaddings(2);
		this.cubePanel = cubePanel;
		
		loadPropertiesSelection();
		loadActions();
		
		mainWindow.setButtons(new Button[]{btSave, btCancel});
		initWidget(mainWindow);
		mainWindow.show();
	}
	
	private void loadActions() {
		btSave.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				cubePanel.setRowsSelectedProperties(rPanel.getProperties());
				cubePanel.setColumnsSelectedProperties(cPanel.getProperties());
				cubePanel.setNotSelectedProperties(aPanel.getProperties());
				mainWindow.close();
			}
		});
		
		btCancel.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				mainWindow.close();
			}
		});
	}

	public void loadPropertiesSelection(){
		cPanel = new PropertyPanel("Columnas", this.cubePanel.getColumnsSelectedProperties(), this);
		rPanel = new PropertyPanel("Filas", this.cubePanel.getRowsSelectedProperties(), this);
		aPanel = new AvailablePropertyPanel("Disponibles", this.cubePanel.getNotSelectedProperties(), this);
		mainWindow.add(cPanel, new ColumnLayoutData(300));
		mainWindow.add(rPanel, new ColumnLayoutData(300));
		mainWindow.add(aPanel, new ColumnLayoutData(300));	
	}

	public List<String> getColumnProperties() {
		return cPanel.getProperties();
	}

	public List<String> getRowProperties() {
		return rPanel.getProperties();
	}
	
	public List<String> getProperties(){
		List<String> properties = new ArrayList<String>();
		properties.addAll(this.getColumnProperties());
		properties.addAll(this.getRowProperties());
		return properties;
	}

	public void freeProperty(String propertyName) {
		this.aPanel.addProperty(propertyName);
	}

	public String getLabelForProperty(String propertyName) {
		return this.cubePanel.getLabelForProperty(propertyName);
	}

	public void moveToColumnProperties(String propertyName) {
		cPanel.addProperty(propertyName);
	}

	public void moveToRowProperties(String propertyName) {
		rPanel.addProperty(propertyName);
	}
	

}
